package com.zdsoft.blp.gongyong.net;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by Administrator on 2016/8/29.
 */
public class ThreadPool {
    private static ThreadPool p = null;
    private static ExecutorService service;
    private ThreadPool() {
    }

    /**
     * 返回线程池本身
     * @return
     */
    public static synchronized ThreadPool getInstance() {
        if(p==null){
            p=new ThreadPool();
            //获取手机cpu的数量
            int cpuNum=Runtime.getRuntime().availableProcessors();
            //创建固定数量的线程
            service=Executors.newFixedThreadPool(cpuNum*2);
        }
        return p;
    }

    /**
     * 执行我传过来的任务
     */
    public void addTask(Runnable runnable){
        service.execute(runnable);
    }
}
